<?php

namespace App\Service\ChatGroup;

use App\Library\FeiShu\SendFs;
use App\Models\ChatGroup\ChatGroupUserModel;
use App\Models\Common\ConfigModel;
use App\Models\Shop\ShopModel;
use App\Service\BaseService;
use App\Service\Rms\ShopService;
use Exception;
use Kuafu\GeneralFs\BuildFs;
use Kuafu\GeneralFs\CardStyle\FsCardGrid;
use Kuafu\GeneralFs\FsCardEstablish;

class ChatGroupNotifyService extends BaseService
{
    /**
     * 人工客服通知群
     *
     * @param int $shopId
     * @param int $groupId
     * @return void
     * @throws Exception
     */
    public static function callServeFsNotify(int $shopId, int $groupId)
    {
        // 获取客服群里的人
//        $userIds = ChatGroupUserModel::query()
//                                     ->where('shopId', $shopId)
//                                     ->where('tag', ChatGroupUserModel::TAG_CUSTOMER_SERVICE)
//                                     ->pluck('userId')->toArray();

        $elements = self::getSendElements($shopId);

        $url = self::pcUrl("customer_server_chat_group/list?groupId={$groupId}&serveIn=2");

        $card = (new FsCardEstablish())
            ->setHeader('blue', '客服呼叫')
            ->pushElements($elements)
            ->pushElements(['tag' => 'hr'])
            ->setButton('查看', $url)
            ->getTemplateArr();

        BuildFs::newRobot(SendFs::ROBOT_WHO_BOT_AI_CALLING, BuildFs::MSG_TYPE_CARD)
               ->setCardContent($card)
               ->send();

//        BuildFs::newRobot('center', BuildFs::MSG_TYPE_CARD, BuildFs::MODULE_TYPE_APP)
//               ->setCardContent($card)
//               ->setUserIds($userIds)
//               ->send();
    }

    /**
     * 转接呼叫顾问
     *
     * @param int $shopId
     * @param int $groupId
     * @param int $tag
     * @return void
     * @throws Exception
     */
    public static function callServeCounselor(int $shopId, int $groupId, int $tag)
    {
        // 获取客服群里的的收银顾问
        $userIds = ChatGroupUserModel::query()
                                     ->where('shopId', $shopId)
                                     ->where('tag', $tag)
                                     ->pluck('userId')->toArray();

        if (count($userIds) == 0) {
            return;
        }

        $elements = self::getSendElements($shopId);

        $url = self::pcUrl("customer_server_chat_group/list?groupId={$groupId}&serveIn=1");

        $card = (new FsCardEstablish())
            ->setHeader('blue', '客服呼叫')
            ->pushElements($elements)
            ->pushElements(['tag' => 'hr'])
            ->setButton('查看', $url)
            ->getTemplateArr();

        BuildFs::newRobot('center', BuildFs::MSG_TYPE_CARD, BuildFs::MODULE_TYPE_APP)
               ->setCardContent($card)
               ->setUserIds($userIds)
               ->send();
    }

    public static function pcUrl(string $path): string
    {
        $appid = ConfigModel::getFsConfigByPlatformNameOrgId('center', 0)['appId'] ?? '';

        return "https://applink.feishu.cn/client/web_app/open?appId={$appid}&path=$path";
    }

    /**
     * @param int $shopId
     * @return array
     */
    public static function getSendElements(int $shopId): array
    {
        $shopInfo = (new \App\Models\Shop\ShopModel)->getDataById($shopId);

        return (new FsCardGrid())
            ->pushTd("**门店编号：** {$shopInfo['shopCode']}", false)
            ->pushTd("")
            ->pushTd("**所属区域：** {$shopInfo['addressPrefix']}", false)
            ->pushTd("")
            ->pushTd("**门店名称：** {$shopInfo['name']}", false)
            ->pushTd("")
            ->pushTd("**呼叫时间：** " . date('Y-m-d H:i:s'), false)
            ->getElement();
    }
}
